package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.*;
import com.dongdongshop.vo.SpecVO;
import com.dongdongshop.vo.TempplateVO;
import org.apache.commons.codec.language.bm.Lang;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("goodController")
public class GoodsController {

    @Reference
    private ItemCatService itemCatService;

    @Reference
    private TypeTemplateService typeTemplateService;

    @Reference
    private GoodService goodService;

    @Reference
    private SpecificationService specificationService;

    @Reference
    private BrandService brandService;

    @RequestMapping("show")
    public String show(){
        return "admin/goods_edit";
    }

    @RequestMapping("findList")
    @ResponseBody
    public Result findList(@RequestParam("parentId")Long parentId){
        try{
            List<TbItemCat> list = itemCatService.itemCatList(parentId);
            return Result.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("getTypeId")
    @ResponseBody
    public Result getTypeId(@RequestParam("id")Long id){
        try{
            TbItemCat list = itemCatService.updateById(id);
            return Result.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("selectBrand")
    @ResponseBody
    public Result selectBrand(@RequestParam("id")Long id){
        try{
            //通过模板id查询规格信息
            TbTypeTemplate type = typeTemplateService.getTypeById(id);
            TempplateVO vo = new TempplateVO();
            vo.setTypeTemplate(type);
            //解析模板中规格的数据
            List<Map> specList = JSONObject.parseArray(type.getSpecIds(), Map.class);
            List<TbTypeTemplate> collect = specList.stream().map(map -> {
                Integer id1 =(Integer) map.get("id");
                String name = (String) map.get("text");
                TbTypeTemplate spec = new TbTypeTemplate();
                spec.setId(id1.longValue());
                spec.setName(name);
                return spec;
            }).collect(Collectors.toList());

            List<SpecVO> collect1 = collect.stream().map(s->specificationService.getSpecById(s.getId())).collect(Collectors.toList());
            vo.setSpecVOList(collect1);
            return Result.response(ResultEnum.SUCCESS).setData(vo);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,TbItem tbItem,BigDecimal itmePrice,Long itemId,Long tbBrandId){
        try{
            TbSeller tbSeller = (TbSeller) SecurityUtils.getSubject().getPrincipal();
            //获取商家id
            tbGoods.setSellerId(tbSeller.getSellerId());
            //默认审核状态
            tbGoods.setAuditStatus("0");
            //默认上下架
            tbGoods.setIsMarketable("0");
            //默认是否删除
            tbGoods.setIsDelete("0");

            //副标题
            tbItem.setSellPoint(tbGoods.getCaption());
            //价格
            tbItem.setPrice(itmePrice);
            //现有库存
            tbItem.setStockCount(tbItem.getNum());
            //商品状态
            tbItem.setStatus("1");
            //创建时间
            tbItem.setCreateTime(new Date());
            //更新时间
            tbItem.setUpdateTime(new Date());
            //商家id
            tbItem.setSellerId(tbSeller.getSellerId());
            //查询类目
            TbItemCat tbItemCat = itemCatService.updateById(itemId);
            tbItem.setCategory(tbItemCat.getName());
            //商家
            tbItem.setSeller(tbSeller.getNickName());
            //品牌
            TbBrand brandById = brandService.getBrandById(tbBrandId);
            tbItem.setBrand(brandById.getName());
            Boolean i = goodService.addGoods(tbGoods,tbGoodsDesc,tbItem);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }
}
